package com.ccl.iot.huiju.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.SparseArray;

import com.ccl.iot.batch.IOTBatch;
import com.ccl.iot.bean.IOTHuiJuBean;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.huiju.IOTHuiJu;
import com.ccl.iot.huiju.db.IOTDaoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：
 * 作者：HD on 2017/10/27 09:47
 * 邮箱：hehandong@qq.com
 */

public class HuiJuUtil {

    public static SparseArray<IOTHuiJu> getPageData(int dataPageIndex, ArrayList<IOTHuiJu> iotHuiJuList) {

        if (iotHuiJuList != null) {

            SparseArray<IOTHuiJu> dataList = new SparseArray<>();
            int allHuijuCount = iotHuiJuList.size();

            List<IOTHuiJuBean> huiJuDBList = IOTDaoUtil.queryDataFromPage(dataPageIndex);
            int dbSize = huiJuDBList.size();

            for (int i = 0; i < dbSize; i++) {
                IOTHuiJuBean iotHuiJuDB = huiJuDBList.get(i);
                if (iotHuiJuDB != null) {
                    int positionIndex = iotHuiJuDB.getPositionIndex();
                    String beanIOTId = iotHuiJuDB.getIOTId();

                    for (int j = 0; j < allHuijuCount; j++) {
                        IOTHuiJu iotHuiJu = iotHuiJuList.get(j);
                        if (iotHuiJu instanceof IOTDevice) {
                            IOTDevice iotDevice = (IOTDevice) iotHuiJu;
                            String udid = iotDevice.getUDID();
                            if (udid.equals(beanIOTId)) {
                                dataList.put(positionIndex, iotHuiJu);
                            }
                        } else if (iotHuiJu instanceof IOTBatch) {
                            IOTBatch iotBatch = (IOTBatch) iotHuiJu;
                            long id = iotBatch.getID();
                            String batchID = String.valueOf(id);
                            if (batchID.equals(beanIOTId)) {
                                dataList.put(positionIndex, iotHuiJu);
                            }
                        }
                    }
                }
            }
            return dataList;
        }
        return null;
    }


    public static void initAllPositionData(ArrayList<IOTHuiJu> iotHuiJuList) {

        if (iotHuiJuList != null) {

            int allHuijuCount = iotHuiJuList.size();
            //查询主页数据库所有设备
            List<IOTHuiJuBean> huiJuDBList = IOTDaoUtil.queryAllData();
            if (huiJuDBList != null){

                int size = huiJuDBList.size();

                for (int i = 0; i < size; i++) {
                    IOTHuiJuBean iotHuiJuDB = huiJuDBList.get(i);
                    if (iotHuiJuDB != null) {
                        int positionIndex = iotHuiJuDB.getPositionIndex();
                        int pageIndex = iotHuiJuDB.getPageIndex();
                        String beanIOTId = iotHuiJuDB.getIOTId();

                        for (int j = 0; j < allHuijuCount; j++) {
                            IOTHuiJu iotHuiJu = iotHuiJuList.get(j);
                            if (iotHuiJu instanceof IOTDevice) {
                                IOTDevice iotDevice = (IOTDevice) iotHuiJu;
                                String udid = iotDevice.getUDID();
                                if (udid.equals(beanIOTId)) {
                                    iotDevice.setPageIndex(pageIndex);
                                    iotDevice.setPositionIndex(positionIndex);
                                }
                            } else if (iotHuiJu instanceof IOTBatch) {
                                IOTBatch iotBatch = (IOTBatch) iotHuiJu;
                                long id = iotBatch.getID();
                                String batchID = String.valueOf(id);
                                if (batchID.equals(beanIOTId)) {
                                    iotBatch.setPageIndex(pageIndex);
                                    iotBatch.setPositionIndex(positionIndex);
                                }
                            }
                        }
                    }
                }
            }

        }
    }

    /**
     * get App versionCode
     * @param context
     * @return
     */
    public static String getVersionCode(Context context){
        PackageManager packageManager=context.getPackageManager();
        PackageInfo packageInfo;
        String versionCode="";
        try {
            packageInfo=packageManager.getPackageInfo(context.getPackageName(),0);
            versionCode=packageInfo.versionCode+"";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * get App versionName
     * @param context
     * @return
     */
    public static String getVersionName(Context context){
        PackageManager packageManager=context.getPackageManager();
        PackageInfo packageInfo;
        String versionName="";
        try {
            packageInfo=packageManager.getPackageInfo(context.getPackageName(),0);
            versionName=packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

}
